﻿using Mock.Dotnet.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace MockNetCore
{
    public class _Function
    {
        private static readonly Random RANDOM = new Random();
        private static readonly String LETTERS = "abcdefghijklmnopqrstuvwxyz";

        /// <summary>
        /// 随机布尔值
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static bool _boolean(String paramStr)
        {
            return RANDOM.Next(0, 2) == 1;
        }

        /// <summary>
        /// 随机自然数
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static int _natural(String paramStr)
        {
            return Math.Abs(_integer(paramStr));
        }

        /// <summary>
        /// 随机整数
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static int _integer(String paramStr)
        {
            if (string.IsNullOrEmpty(paramStr))
            {
                return RANDOM.Next();
            }

            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            if (_params.Length == 1)
            {
                return RANDOM.Next(int.Parse(_params[0].Trim()));
            }

            if (_params.Length == 2)
            {
                return RANDOM.Next(int.Parse(_params[0].Trim()), int.Parse(_params[1].Trim()) + 1);
            }

            return RANDOM.Next();
        }

        /// <summary>
        /// 随机浮点数
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static double _float(String paramStr)
        {
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            double minNum = 0, maxNum = 0;
            if (_params.Length == 1)
            {
                maxNum = double.Parse(_params[0]);
            }
            if (_params.Length >= 2)
            {
                minNum = double.Parse(_params[0]);
                maxNum = double.Parse(_params[1]);
            }
            //小数位数
            int length = 0, minLength = 0, maxLength = 0;
            if (_params.Length == 3)
            {
                maxLength = int.Parse(_params[2]);
            }
            if (_params.Length == 4)
            {
                minLength = int.Parse(_params[2]);
                maxLength = int.Parse(_params[3]);
            }
            if (minLength == 0 && maxLength == 0)
                length = RANDOM.Next(1, 10);
            else
                length = RANDOM.Next(minLength, maxLength + 1);
            if (minNum == 0 && maxNum == 0)
                return RANDOM.Next() + RANDOM.NextDouble();
            return Math.Round(RANDOM.NextDouble() * (maxNum - minNum) + minNum, length);
        }

        /// <summary>
        /// 随机日期时间
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static String _date(String paramStr)
        {
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            string dtmin = null, dtmax = null, formart = null;
            if (_params.Length >= 1)
            {
                formart = _params[0];
            }
            if (_params.Length == 2)
            {
                dtmax = _params[1];
            }
            if (_params.Length == 3)
            {
                dtmin = _params[1];
                dtmax = _params[2];
            }
            DateTime dtRandom = RandomDateUtil.RandomDate(dtmin, dtmax);

            if (string.IsNullOrEmpty(formart))
            {
                return dtRandom.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return dtRandom.ToString(formart);
        }

        /// <summary>
        /// 当前时间
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static String _now(String paramStr)
        {
            if (string.IsNullOrEmpty(paramStr))
            {
                return DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            return DateTime.Now.ToString(paramStr);
        }

        /// <summary>
        /// 随机一个字符
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static char _character(String paramStr)
        {
            String baseChars = string.IsNullOrEmpty(paramStr) ? LETTERS : paramStr;
            return LETTERS.ToCharArray()[RANDOM.Next(baseChars.Length)];
        }

        public static string _string(String paramStr)
        {
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            int min = 1, max = 30;
            string temp = null;
            if (_params.Length >= 1)
            {
                max = int.Parse(_params[0]);
            }
            if (_params.Length >= 2)
            {
                max = int.Parse(_params[1]);
            }
            if (_params.Length == 3)
            {
                temp = _params[2];
            }
            return RandomStringUtil.RandomString(min, max, temp);
        }

        /// <summary>
        /// 随机中文字符
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _cword(String paramStr)
        {
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            int min = 1, max = 30;
            if (_params.Length == 1)
            {
                max = int.Parse(_params[0]);
            }
            if (_params.Length == 2)
            {
                min = int.Parse(_params[0]);
                max = int.Parse(_params[1]);
            }
            return RandomStringUtil.RandomChineseWords(RANDOM.Next(min, max + 1));
        }

        /// <summary>
        /// 随机英文名
        /// </summary>
        /// <returns></returns>
        public static string _first(String paramStr)
        {
            return RandomStringUtil.RandomFirstName();
        }

        /// <summary>
        /// 随机英文姓
        /// </summary>
        /// <returns></returns>
        public static string _last(String paramStr)
        {
            return RandomStringUtil.RandomLastName();
        }

        /// <summary>
        /// 随机英文名
        /// </summary>
        /// <returns></returns>
        public static string _name(String paramStr)
        {
            return RandomStringUtil.RandomName();
        }

        /// <summary>
        /// 随机中文姓
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _clast(String paramStr)
        {
            return RandomStringUtil.RandomCSureName();
        }

        /// <summary>
        /// 随机中文名字
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _cfirst(String paramStr)
        {
            return RandomStringUtil.RandomCFirstName();
        }

        /// <summary>
        /// 随机中文姓名
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _cname(String paramStr)
        {
            return RandomStringUtil.RandomCName();
        }

        /// <summary>
        /// 随机顶级域名
        /// </summary>
        /// <returns></returns>
        public static string _tld(String paramStr)
        {
            return RandomStringUtil.RandomTld();
        }

        /// <summary>
        /// 随机协议
        /// </summary>
        /// <returns></returns>
        public static string _protocol(String paramStr)
        {
            return RandomStringUtil.RandomProtocol();
        }

        /// <summary>
        /// 随机域名
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _domain(String paramStr)
        {
            return RandomStringUtil.RandomString(1, 10) + '.' + (string.IsNullOrEmpty(paramStr) ? RandomStringUtil.RandomTld() : paramStr);
        }

        /// <summary>
        /// 随机url
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _url(String paramStr)
        {
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            string protocol = null, host = null;
            if (_params.Length == 1)
            {
                protocol = _params[0];
            }
            if (_params.Length == 2)
            {
                protocol = _params[0];
                host = _params[1];
            }
            return (protocol ?? RandomStringUtil.RandomProtocol()) + "://" + (host ?? _domain("")) + "/" + RandomStringUtil.RandomString(1, 10);
        }

        /// <summary>
        /// 随机邮箱
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static string _email(String paramStr)
        {
            return RandomStringUtil.RandomString(1, 20) + "@" + (string.IsNullOrEmpty(paramStr) ? _domain(null) : paramStr);
        }

        /// <summary>
        /// 随机ip
        /// </summary>
        /// <returns></returns>
        public static string _ip(String paramStr)
        {
            return RANDOM.Next(0, 256) + "." +
               RANDOM.Next(0, 256) + "." +
               RANDOM.Next(0, 256) + "." +
               RANDOM.Next(0, 256);
        }

        /// <summary>
        /// 随机大区
        /// </summary>
        /// <returns></returns>
        public static string _region(String paramStr)
        {
            return RandomStringUtil.RandomRegion();
        }

        /// <summary>
        /// 随机省、直辖市、自治区、特别行政区
        /// </summary>
        /// <returns></returns>
        public static string _province(String paramStr)
        {
            return RandomStringUtil.RandomRegion();
        }

        /// <summary>
        /// 随机市
        /// </summary>
        /// <returns></returns>
        public static string _city(String paramStr)
        {
            bool pre = Boolean.Parse(string.IsNullOrEmpty(paramStr) ? "false" : paramStr);
            return RandomStringUtil.RandomCity(pre);
        }

        /// <summary>
        /// 随机县
        /// </summary>
        /// <returns></returns>
        public static string _county(String paramStr)
        {
            bool pre = Boolean.Parse(string.IsNullOrEmpty(paramStr) ? "false" : paramStr);
            return RandomStringUtil.RandomCounty(pre);
        }

        /// <summary>
        /// 随机邮编
        /// </summary>
        /// <returns></returns>
        public static string _zip(String paramStr)
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < 6; i++)
            {
                sb.Append(RANDOM.Next(0, 10));
            }
            return sb.ToString();
        }

        /// <summary>
        /// 数字范围
        /// </summary>
        /// <param name="paramStr"></param>
        /// <returns></returns>
        public static JArray _range(String paramStr)
        {
            int start = 0, stop = 0, step = 1;
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            if (_params.Length == 1)
            {
                stop = int.Parse(_params[0]);
            }
            if (_params.Length >= 2)
            {
                start = int.Parse(_params[0]);
                stop = int.Parse(_params[1]);
            }
            if (_params.Length == 3)
            {
                step = int.Parse(_params[2]);
            }
            List<int> res = new List<int>();
            for (int i = start; i <= stop; i += step)
            {
                res.Add(i);
            }
            return JArray.FromObject(res);
        }

        public static JArray _drange(String paramStr)
        {
            DateTime start = DateTime.Parse("1970-01-01 00:00:00"), stop = DateTime.Parse("2099-12-31 00:00:00");
            string type = "yyyy", fmt = "yyyy-MM-dd HH:mm:ss";
            int step = 1;
            String[] _params = paramStr.Split(new char[] { ',' }).Where(m => !string.IsNullOrEmpty(m)).ToArray();
            if (_params.Length == 1)
            {
                stop = DateTime.Parse(_params[0]);
            }
            if (_params.Length >= 2)
            {
                start = DateTime.Parse(_params[0]);
                stop = DateTime.Parse(_params[1]);
            }
            if (_params.Length >= 3)
            {
                step = int.Parse(_params[2]);
            }
            if (_params.Length >= 4)
            {
                type = _params[3];
            }
            if (_params.Length == 5)
            {
                fmt = _params[4];
            }
            List<string> res = new List<string>();
            while (start <= stop)
            {
                res.Add(start.ToString(fmt));
                switch (type)
                {
                    case "yyyy":
                    default:
                        start = start.AddYears(step);
                        break;

                    case "MM":
                        start = start.AddMonths(step);
                        break;

                    case "dd":
                        start = start.AddDays(step);
                        break;

                    case "HH":
                        start = start.AddHours(step);
                        break;

                    case "mm":
                        start = start.AddMinutes(step);
                        break;

                    case "ss":
                        start = start.AddSeconds(step);
                        break;
                }
            }
            return JArray.FromObject(res);
        }
    }
}